/**
 * 
 */
package de.preamp.clients.java.utils.ui;

import java.awt.Frame;
import java.awt.GridLayout;
import java.io.IOException;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.json.JSONException;

import de.preamp.clients.java.net.ServerConnection;
import de.preamp.clients.java.net.ServerConnectionImpl;
import de.preamp.clients.java.utils.Tools;
import de.preamp.clients.java.utils.UpdateStatus;

/**
 * @author André Stannek
 * @since 2010-03-21
 * @version 2010-08-22
 */
public class ScanDialog extends JDialog implements Runnable
{
    /**
     * Genrated UID.
     */
    private static final long serialVersionUID = -6246775288769054495L;

    /**
     * 
     */
    private final transient JLabel info1;

    /**
     * 
     */
    private final transient JLabel info2;

    /**
     * @param owner
     *            the owner of this dialog
     */
    public ScanDialog(final Frame owner)
    {
	super(owner, false);
	this.setResizable(false);
	this.setModal(false);
	final int height = 200;
	final int width = 600;

	final JPanel content = new JPanel(new GridLayout(2, 1));

	info1 =
		new JLabel("Library on server is updating. "
			+ "You can close this window at any time");
	info2 = new JLabel();
	this.add(info1);
	this.add(info2);

	final Thread thread = new Thread(this);
	thread.start();

	this.setContentPane(content);

	// TODO Handle window closing correctly.
	// this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

	this.setBounds(Tools.getCenteredBounds(width, height));
	this.setVisible(true);
    }

    @Override
    public final void run()
    {
	final ServerConnection serverConnection = new ServerConnectionImpl();
	try
	{
	    serverConnection.startUpdate();
	}
	catch (final IOException e)
	{
	    JOptionPane.showMessageDialog(null,
		    "I/O error occured on update progress", "IOException",
		    JOptionPane.ERROR_MESSAGE);
	}
	catch (final JSONException e)
	{
	    JOptionPane
		    .showMessageDialog(
			    null,
			    "JSON-Error occured during update progress. This normaly means that the server provided a wrong result.",
			    "JSONException", JOptionPane.ERROR_MESSAGE);
	}

	String status = "";
	UpdateStatus updateStatus;
	do
	{
	    try
	    {
		updateStatus = serverConnection.updateStatus();
		status = updateStatus.getStatus();
		info2.setText("Overall Files: " + updateStatus.getTotal()
			+ ", New Files: " + updateStatus.getAdded()
			+ ", Removed Files: " + updateStatus.getRemoved());
	    }
	    catch (final IOException e)
	    {
		JOptionPane.showMessageDialog(null,
			"I/O error occured on update progress", "IOException",
			JOptionPane.ERROR_MESSAGE);
	    }
	    catch (final JSONException e)
	    {
		JOptionPane
			.showMessageDialog(
				null,
				"JSON-Error occured during update progress. This normaly means that the server provided a wrong result.",
				"JSONException", JOptionPane.ERROR_MESSAGE);
	    }
	}
	while ("working".equals(status));
	info1.setText("Update finished");
    }
}
